//
//  SudokuCaculator.m
//  TaShuo
//
//  Created by N-X on 2018/1/19.
//  Copyright © 2018年 MRNAN. All rights reserved.
//  九宫格布局计算

#import "SudokuCaculator.h"

@interface SudokuCaculator()
@property (nonatomic ,strong)NSMutableArray * itemsFrameArray;
@end

@implementation SudokuCaculator

+ (instancetype)shareInstance
{
    static SudokuCaculator * caculator = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        caculator = [[SudokuCaculator alloc]init];
    });
    return caculator;
}

#pragma mark - utils(TODO：考虑怎么可以把九宫格的算法单独抽出来)
// 可以用一个数组，把每个imageView的frame存起来，然后给外部去用

- (CGSize)caculatorSizeWithPicArray:(NSArray *)picArray isJustCaculator:(BOOL)isJustCaculator
{
//    if (picArray.count == 0) {
//        return CGSizeZero;
//    }
//
//    CGFloat itemW = [self itemWidthForPicPathArray:picArray];
//    CGFloat itemH = 0;
//    if (picArray.count == 1) {
//        UIImage *image = [UIImage imageNamed:picArray.firstObject];
//        if (image.size.width) {
//            itemH = image.size.height / image.size.width * itemW;
//        }
//    } else {
//        itemH = itemW;
//    }
//    long perRowItemCount = [self perRowItemCountForPicPathArray:picArray];
//    CGFloat margin = 5;
//
//    _itemsFrameArray = [NSMutableArray array];
//
//    [picArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        long columnIndex = idx % perRowItemCount;
//        long rowIndex = idx / perRowItemCount;
//        if (!isJustCaculator) {
//            CGRect frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
//            NSValue *frameValue = [NSValue valueWithCGRect:frame];
//            [_itemsFrameArray addObject:frameValue];
//        }
//    }];
//
//    CGFloat w = perRowItemCount * itemW + (perRowItemCount - 1) * margin;
//    int columnCount = ceilf(picArray.count * 1.0 / perRowItemCount);
//    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
//
//    return CGSizeMake(w, h);
    
   return  [self caculatorSizeWithPicArray:picArray containerWidth:0 isJustCaculator:isJustCaculator];
}


- (CGSize)caculatorSizeWithPicArray:(NSArray *)picArray containerWidth:(CGFloat)containerWidth isJustCaculator:(BOOL)isJustCaculator
{
    if (picArray.count == 0) {
        return CGSizeZero;
    }
    
    CGFloat margin = 5;
    long perRowItemCount = [self perRowItemCountForPicPathArray:picArray];

    // 这里先写死的都是除以3的宽度
    CGFloat itemW = containerWidth != 0?(containerWidth - margin * (perRowItemCount -1)) / 3 :[self itemWidthForPicPathArray:picArray];
    CGFloat itemH = 0;
    if (picArray.count == 1) {
        UIImage *image = [UIImage imageNamed:picArray.firstObject];
        if (image.size.width) {
            itemH = image.size.height / image.size.width * itemW;
        }
    } else {
        itemH = itemW;
    }
    
    _itemsFrameArray = [NSMutableArray array];
    
    [picArray enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        long columnIndex = idx % perRowItemCount;
        long rowIndex = idx / perRowItemCount;
        if (!isJustCaculator) {
            CGRect frame = CGRectMake(columnIndex * (itemW + margin), rowIndex * (itemH + margin), itemW, itemH);
            NSValue *frameValue = [NSValue valueWithCGRect:frame];
            [_itemsFrameArray addObject:frameValue];
        }
    }];
    
    CGFloat w = containerWidth != 0 ?containerWidth :perRowItemCount * itemW + (perRowItemCount - 1) * margin;
    int columnCount = ceilf(picArray.count * 1.0 / perRowItemCount);
    CGFloat h = columnCount * itemH + (columnCount - 1) * margin;
    
    return CGSizeMake(w, h);
}

- (CGFloat)itemWidthForPicPathArray:(NSArray *)array
{
    if (array.count == 1) {
        return 120;
    } else {
        CGFloat w = [UIScreen mainScreen].bounds.size.width > 320 ? 80 : 70;
        return w;
    }
}

- (NSInteger)perRowItemCountForPicPathArray:(NSArray *)array
{
    if (array.count < 3) {
        return array.count;
    } else if (array.count <= 4) {
        return 2;
    } else {
        return 3;
    }
}

- (NSArray<NSValue *> *)itemFrameArray
{
    return self.itemsFrameArray;
}

@end
